package cl.mt.util.servlet.comun;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import cl.mt.data.DataPais;
import cl.mt.data.DataRegion;
import cl.mt.dto.DTOPais;
import cl.mt.dto.DTORegion;

/**
 * Servlet implementation class RegionServlet
 */
public class RegionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	final Logger log = LogManager.getLogger(this.getClass()); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("en region servlet");
		
		//regionId
		int id =Integer.parseInt(request.getParameter("paisId"));
		//int id=22;
		log.debug("id pais"+ id);
		DataRegion dtaRegion = new DataRegion();
		Collection<DTORegion> collRegion = dtaRegion.findByPais((long) id);
		 response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<option value='0'>Seleccione </option>");
		try{
			for(DTORegion region : collRegion){
				 out.printf("<option value=\"%1s\">%2s</option>", region.getId(), region.getDescripcion());
		            
			}
		}finally {
            out.close();
		}
	}

}
